/*
@tips 三层切换，1分类 2 规格 3 报价方式
@tips 商品货号goods_code 跟 规格
@tips 商品底价floor_price 跟 报价方式
@tips 起订量start_num  按规格报价 才显示，按数量报价模板已有
@author Blade
*/
$(function() {
	/*输入过滤*/
	$('input[name="goods_name"]').bind('keyup', function() {
		var num = 50 - $(this).val().length;
		var str = '还能输入' + num + '个字';
		if (num > 0) {
			$('span.goods-tips').html(str);
		} else if (num == 0) {
			goodcreate.title = $(this).val();
			$('span.goods-tips').html(str);
		} else {
			str = '还能输入0个字';
			$('span.goods-tips').html(str);
			$(this).val(goodcreate.title);
		}
	});

	$('#gcategory_edit').bind('click',function(){
		wl.sConfirm({'info':'重新编辑将清空商品属性、规格等数据，继续吗？','success':function(){
			$('#gcategory_edit').parent('p').addClass('gcategory_edit_click');
			$('#gcategory > .f_l_tips').show();
			$('#gcategory > .f_l_title').hide();
			$('#gcategory_edit').hide();
			$("#goods-attr-list").hide();
			$('#goods-spec-list').hide();
			$('#goods_info_wrap').html(goods_info_content);
		},'fail':function(){
			// 
		}});
	});

	/*1商品分类切换*/
	gcategoryInit("gcategory");

	/*2产品规格切换*/
	var goodcreate = {
		prodFormat: $('#goods-format-prod'), //产品规格div
		saleInfo: $('#goods-sale-info'), //销售信息tr
		priceFrom: $('#goods-price-table'), //按数量--价格区间table ,更新切换
		saleNum: $('#goods-sale-num'), //按数量--可售数量dl ,更新切换
		saleFormat: $('#goods-sale-format'), //按规格--可售数量dl ,更新切换
		tatalNum: $('#goods-tatalNum'), //总价input  ,更新切换
		title: "",
		scale: 0,
		yanseInputLen: 0,
		formatInputLen: 0,
		inputsLens: 0,
		numTotal: 0,
		formatTotal: 0,
		clone1: '',
		cloneLeft: null,
		cloneRight: null
	};
	goodcreate.prodFormat.bind('click', function(event) {
		var current = $(event.target);
		if (current.is('input')) {
			//规格切换时去重
			var this_value = current.val();
			var all_inputs = $(this).find('input:checked');
			var nextStep = 0;
			$.each(all_inputs, function(i) {
				if ($(this).val() == this_value) {
					nextStep++;
				}
			});
			if (nextStep > 1) {
				current.attr('checked', false);
				alert('规格值重复，请重新选择！');
				return;
			}
			var yanseInput = $(this).children('dl:eq(0)').find('input:checked');
			var formatInput = $(this).children('dl:eq(1)').find('input:checked');
			var listTitle, listOtherTitle;
			var goods_info_wrap = $('#goods_info_wrap');
			goodcreate.yanseInputLen = yanseInput.length;
			goodcreate.formatInputLen = formatInput.length;
			goodcreate.inputsLens = yanseInput.length + formatInput.length;
			listTitle = $(this).children('dl:eq(0)').children('dt').find('input').val();
			if ($(this).children('dl:eq(1)').children('dt').children('em').length > 0) {
				listOtherTitle = $(this).children('dl:eq(1)').children('dt').find('input').val();
			}
			var goods_pricetype_a = $('#goods-pricetype-a'),
				pricetype_b_wrap = $('#pricetype_b_wrap'),
				goods_pricetype_b = $('#goods-pricetype-b'),
				saleNum = $('#goods-sale-num'), //按数量--可售数量dl ,更新切换
				saleFormat = $('#goods-sale-format'), //按规格--可售数量dl ,更新切换
				saleNumDd = $('#goods-sale-num > dd'), //按数量--可售数量dd ,更新切换
				saleFormatDd = $('#goods-sale-format > dd'), //按规格--可售数量dd ,更新切换
				goods_code = $('#goods_code'), //商品货号，没规格出现，有规格就清空
				tatalNum = $('#goods-tatalNum'); //总价input  ,更新切换
			var otable = null,
				otr = null,
				othertable = null,
				othertr = null,
				text = '',
				textoter = '';
			if (goodcreate.yanseInputLen > 0 && goodcreate.formatInputLen == 0) {
				otable = $('<table><tr><th width="60">' + listTitle + '</th><th><i>*</i>库存</th><th>单品货号</th></tr></table>');
				othertable = $('<table><tr><th width="60">' + listTitle + '</th><th><i>*</i>零售价</th><th>市场价</th><th><i>*</i>库存</th><th>单品货号</th></tr></table>');
				// 遍历选中规格
				yanseInput.each(function(i) {
					text = $.trim($(this).siblings('label').html());
					var q, len, bool = true;
					//遍历数据缓存，有数据就绑数据，没数据按bool为true执行
					for (q = 0, len = SPEC.specs.length; q < len; q++) {
						if (SPEC.specs[q].spec_1 === text) {
							bool = false;
							otr = $('<tr><td>' + text + '<input type="hidden" name="spec_1[' + SPEC.specs[q].spec_id + ']" value="' + text + '" /></td><td><input type="text" name="stock[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].stock + '" data="num"  class="{required:true,messages:{required:\'不能为空\'}}" /></td><td><input name="sku[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].sku + '" /><input type="hidden" name="spec_id[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].spec_id + '" /></td></tr>');
							othertr = $('<tr><td>' + text + '<input type="hidden" name="spec_1[' + SPEC.specs[q].spec_id + ']" value="' + text + '" /></td><td><input type="text" name="price[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].price + '" class="{required:true,messages:{required:\'不能为空\'}}" onblur="creat_market_price(this)" /></td><td><input type="text" name="market_price[' + SPEC.specs[q].spec_id + ']" value="'+SPEC.specs[q].market_price+'" onblur="zNum(this)" /></td><td><input type="text" name="stock[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].stock + '" data="formatnum" class="{required:true,messages:{required:\'不能为空\'}}" /></td><td><input name="sku[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].sku + '" /><input type="hidden" name="spec_id[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].spec_id + '" /></td></tr>');
							break;
						}
					}
					if (bool) {
						otr = $('<tr><td>' + text + '<input type="hidden" name="spec_1[' + 'add' + i + ']" value="' + text + '" /></td><td><input type="text" name="stock[' + 'add' + i + ']" data="num"  class="{required:true,messages:{required:\'不能为空\'}}" /></td><td><input name="sku[' + 'add' + i + ']" /><input type="hidden" name="spec_id[' + 'add' + i + ']" /></td></tr>');
						othertr = $('<tr><td>' + text + '<input type="hidden" name="spec_1[' + 'add' + i + ']" value="' + text + '" /></td><td><input type="text" name="price[' + 'add' + i + ']" class="{required:true,messages:{required:\'不能为空\'}}" onblur="creat_market_price(this)" /></td><td><input type="text" name="market_price[' + 'add' + i + ']" onblur="zNum(this)" /></td><td><input type="text" name="stock[' + 'add' + i + ']" data="formatnum" class="{required:true,messages:{required:\'不能为空\'}}" /></td><td><input name="sku[' + 'add' + i + ']" /><input type="hidden" name="spec_id[' + 'add' + i + ']" /></td></tr>');
					}
					otable.append(otr);
					othertable.append(othertr);
				});
			} else if (goodcreate.yanseInputLen == 0 && goodcreate.formatInputLen > 0) {
				otable = $('<table><tr><th width="60">' + listOtherTitle + '</th><th><i>*</i>库存</th><th>单品货号</th></tr></table>');
				othertable = $('<table><tr><th width="60">' + listOtherTitle + '</th><th><i>*</i>零售价</th><th>市场价</th><th><i>*</i>库存</th><th>单品货号</th></tr></table>');
				formatInput.each(function(i) {
					text = $.trim($(this).siblings('label').html());
					var q, len, bool = true;
					for (q = 0, len = SPEC.specs.length; q < len; q++) {
						if (SPEC.specs[q].spec_2 === text) {
							bool = false;
							otr = $('<tr><td>' + text + '<input type="hidden" name="spec_2[' + SPEC.specs[q].spec_id + ']" value="' + text + '" /></td><td><input type="text" name="stock[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].stock + '" data="num"  class="{required:true,messages:{required:\'不能为空\'}}" /></td><td><input name="sku[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].sku + '" /><input type="hidden" name="spec_id[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].spec_id + '" /></td></tr>');
							othertr = $('<tr><td>' + text + '<input type="hidden" name="spec_2[' + SPEC.specs[q].spec_id + ']" value="' + text + '" /></td><td><input type="text" name="price[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].price + '" class="{required:true,messages:{required:\'不能为空\'}}" onblur="creat_market_price(this)" /></td><td><input type="text" name="market_price[' + SPEC.specs[q].spec_id + ']" value="'+SPEC.specs[q].market_price+'" onblur="zNum(this)" /></td><td><input type="text" name="stock[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].stock + '" data="formatnum" class="{required:true,messages:{required:\'不能为空\'}}" /></td><td><input name="sku[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].sku + '" /><input type="hidden" name="spec_id[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].spec_id + '" /></td></tr>');
							break;
						}
					}
					if (bool) {
						otr = $('<tr><td>' + text + '<input type="hidden" name="spec_2[' + 'add' + i + ']" value="' + text + '" /></td><td><input type="text" name="stock[' + 'add' + i + ']" data="num"  class="{required:true,messages:{required:\'不能为空\'}}" /></td><td><input name="sku[' + 'add' + i + ']" /><input type="hidden" name="spec_id[' + 'add' + i + ']" /></td></tr>');
						othertr = $('<tr><td>' + text + '<input type="hidden" name="spec_2[' + 'add' + i + ']" value="' + text + '" /></td><td><input type="text" name="price[' + 'add' + i + ']" class="{required:true,messages:{required:\'不能为空\'}}" onblur="creat_market_price(this)" /></td><td><input type="text" name="market_price[' + 'add' + i + ']" onblur="zNum(this)" /></td><td><input type="text" name="stock[' + 'add' + i + ']" data="formatnum" class="{required:true,messages:{required:\'不能为空\'}}" /></td><td><input name="sku[' + 'add' + i + ']" /><input type="hidden" name="spec_id[' + 'add' + i + ']" /></td></tr>');
					}
					otable.append(otr);
					othertable.append(othertr);
				});
			} else if (goodcreate.yanseInputLen > 0 && goodcreate.formatInputLen > 0) {
				otable = $('<table><tr><th width="60">' + listTitle + '</th><th width="60">' + listOtherTitle + '</th><th><i>*</i>库存</th><th>单品货号</th></tr></table>');
				othertable = $('<table><tr><th width="60">' + listTitle + '</th><th width="60">' + listOtherTitle + '</th><th><i>*</i>零售价</th><th>市场价</th><th><i>*</i>库存</th><th>单品货号</th></tr></table>');
				yanseInput.each(function(i) {
					formatInput.each(function(j) {
						text = $.trim(yanseInput.eq(i).siblings('label').html());
						textoter = $.trim(formatInput.eq(j).siblings('label').html());
						var q, len, bool = true;
						for (q = 0, len = SPEC.specs.length; q < len; q++) {
							if (SPEC.specs[q].spec_1 === text && SPEC.specs[q].spec_2 === textoter) {
								bool = false;
								if (j > 0) {
									otr = $('<tr><td>' + textoter + '<input type="hidden" name="spec_1[' + SPEC.specs[q].spec_id + ']" value="' + text + '" /><input type="hidden" name="spec_2[' + SPEC.specs[q].spec_id + ']" value="' + textoter + '" /></td><td><input type="text" name="stock[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].stock + '" data="num"  class="{required:true,messages:{required:\'不能为空\'}}" /></td><td><input name="sku[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].sku + '" /><input type="hidden" name="spec_id[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].spec_id + '" /></td></tr>');
									othertr = $('<tr><td>' + textoter + '<input type="hidden" name="spec_1[' + SPEC.specs[q].spec_id + ']" value="' + text + '" /><input type="hidden" name="spec_2[' + SPEC.specs[q].spec_id + ']" value="' + textoter + '" /></td><td><input type="text" name="price[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].price + '" class="{required:true,messages:{required:\'不能为空\'}}" onblur="creat_market_price(this)" /></td><td><input type="text" name="market_price[' + SPEC.specs[q].spec_id + ']" value="'+SPEC.specs[q].market_price+'" onblur="zNum(this)" /></td><td><input type="text" name="stock[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].stock + '" data="formatnum" class="{required:true,messages:{required:\'不能为空\'}}" /></td><td><input name="sku[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].sku + '" /><input type="hidden" name="spec_id[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].spec_id + '" /></td></tr>');
								} else {
									otr = $('<tr><td rowspan="' + goodcreate.formatInputLen + '">' + text + '</td><td>' + textoter + '<input type="hidden" name="spec_1[' + SPEC.specs[q].spec_id + ']" value="' + text + '" /><input type="hidden" name="spec_2[' + SPEC.specs[q].spec_id + ']" value="' + textoter + '" /></td><td><input type="text" name="stock[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].stock + '" data="num"  class="{required:true,messages:{required:\'不能为空\'}}" /></td><td><input name="sku[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].sku + '" /><input type="hidden" name="spec_id[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].spec_id + '" /></td></tr>');
									othertr = $('<tr><td rowspan="' + goodcreate.formatInputLen + '">' + text + '</td><td>' + textoter + '<input type="hidden" name="spec_1[' + SPEC.specs[q].spec_id + ']" value="' + text + '" /><input type="hidden" name="spec_2[' + SPEC.specs[q].spec_id + ']" value="' + textoter + '" /></td><td><input type="text" name="price[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].price + '" class="{required:true,messages:{required:\'不能为空\'}}" onblur="creat_market_price(this)" /></td><td><input type="text" name="market_price[' + SPEC.specs[q].spec_id + ']" value="'+SPEC.specs[q].market_price+'" onblur="zNum(this)" /></td><td><input type="text" name="stock[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].stock + '" data="formatnum" class="{required:true,messages:{required:\'不能为空\'}}" /></td><td><input name="sku[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].sku + '" /><input type="hidden" name="spec_id[' + SPEC.specs[q].spec_id + ']" value="' + SPEC.specs[q].spec_id + '" /></td></tr>');
								}
								break;
							}
						}
						if (bool) {
							if (j > 0) {
								otr = $('<tr><td>' + textoter + '<input type="hidden" name="spec_1[' + 'add' + i + j + ']" value="' + text + '" /><input type="hidden" name="spec_2[' + 'add' + i + j + ']" value="' + textoter + '" /></td><td><input type="text" name="stock[' + 'add' + i + j + ']" data="num"  class="{required:true,messages:{required:\'不能为空\'}}" /></td><td><input name="sku[' + 'add' + i + j + ']" /><input type="hidden" name="spec_id[' + 'add' + i + j + ']" /></td></tr>');
								othertr = $('<tr><td>' + textoter + '<input type="hidden" name="spec_1[' + 'add' + i + j + ']" value="' + text + '" /><input type="hidden" name="spec_2[' + 'add' + i + j + ']" value="' + textoter + '" /></td><td><input type="text" name="price[' + 'add' + i + j + ']" class="{required:true,messages:{required:\'不能为空\'}}" onblur="creat_market_price(this)" /></td><td><input type="text" name="market_price[' + 'add' + i + j + ']" onblur="zNum(this)" /></td><td><input type="text" name="stock[' + 'add' + i + j + ']" data="formatnum" class="{required:true,messages:{required:\'不能为空\'}}" /></td><td><input name="sku[' + 'add' + i + j + ']" /><input type="hidden" name="spec_id[' + 'add' + i + j + ']" /></td></tr>');
							} else {
								otr = $('<tr><td rowspan="' + goodcreate.formatInputLen + '">' + text + '</td><td>' + textoter + '<input type="hidden" name="spec_1[' + 'add' + i + j + ']" value="' + text + '" /><input type="hidden" name="spec_2[' + 'add' + i + j + ']" value="' + textoter + '" /></td><td><input type="text" name="stock[' + 'add' + i + j + ']" data="num"  class="{required:true,messages:{required:\'不能为空\'}}" /></td><td><input name="sku[' + 'add' + i + j + ']" /><input type="hidden" name="spec_id[' + 'add' + i + j + ']" /></td></tr>');
								othertr = $('<tr><td rowspan="' + goodcreate.formatInputLen + '">' + text + '</td><td>' + textoter + '<input type="hidden" name="spec_1[' + 'add' + i + j + ']" value="' + text + '" /><input type="hidden" name="spec_2[' + 'add' + i + j + ']" value="' + textoter + '" /></td><td><input type="text" name="price[' + 'add' + i + j +']" class="{required:true,messages:{required:\'不能为空\'}}" onblur="creat_market_price(this)" /></td><td><input type="text" name="market_price[' + 'add' + i + j + ']" onblur="zNum(this)" /></td><td><input type="text" name="stock[' + 'add' + i + j + ']" data="formatnum" class="{required:true,messages:{required:\'不能为空\'}}" /></td><td><input name="sku[' + 'add' + i + j + ']" /><input type="hidden" name="spec_id[' + 'add' + i + j + ']" /></td></tr>');
							}
						}
						otable.append(otr);
						othertable.append(othertr);
					});
				});
			}

			/*有规格时*/
			if (goodcreate.inputsLens > 0 && goods_pricetype_a.attr('checked') === true) {
				saleNumDd.empty().append(otable);
				saleNum.show(); //可售数量
			} else if (goodcreate.inputsLens > 0 && goods_pricetype_b.attr('checked') === true) {
				saleFormatDd.empty().append(othertable);
				saleFormat.show();
			}
			if (goodcreate.inputsLens > 0) {
				pricetype_b_wrap.show(); //按规格报价
				goods_code.empty(); //商品货号
				tatalNum.attr('disabled', true).css({
					'background': '#f6f6f6',
					'border-color': '#f6f6f6'
				}).siblings('span').show();
			} else {
				goods_info_wrap.empty().html(goods_info_content);
			}

			/*引用传递，方便移动*/
			goodcreate.cloneLeft = otable;
			goodcreate.cloneRight = othertable;
		}
	});

	/*
	@title 3.1报价切换-初始化缓存
	@tips if(按产品数量报价)
	@totalstock_buffer 缓存在编辑页的可售总量
	*/
	if ($('#floor_price').children().length > 0) {
		goodcreate.clone1 = goodcreate.priceFrom.html(); //价格区间table
		goods_code_buffer = $('#goods_code').html(); //商品货号，按数量报价没规格时
		floor_price_num_buffer = $('#floor_price').html(); //商品底价
		start_num_buffer = start_num_content; //为按规格报价的起订量 初始化Html
	} else {
		goodcreate.clone1 = priceFrom_content; //为按数量报价的价格区间 初始化Html
		goods_code_buffer = goods_code_content; //为按数量报价-且没有规格时的商品货号 初始化Html
		floor_price_num_buffer = floor_price_content; //为按数量报价的商品底价 初始化Html
		start_num_buffer = $('#start_num').html(); //保存按规格报价时的起订量
	}
	goodcreate.numTotal = totalstock_buffer;
	goodcreate.formatTotal = totalstock_buffer;

	/*3.2报价切换*/
	goodcreate.saleInfo.bind('click', function(event) {
		var current = $(event.target);
		var referDiv = $('#goods_info_wrap');
		var saleNumDd = $('#goods-sale-num > dd'), //按数量--可售数量dd ,更新切换
			saleFormatDd = $('#goods-sale-format > dd'), //按规格--可售数量dd ,更新切换
			priceFrom = $('#goods-price-table'),
			pricetype_b = $('#pricetype_b_wrap'),
			floor_price = $('#floor_price'), //按数量显示
			start_num = $('#start_num'), //按规格显示
			tatalNum = $('#goods-tatalNum'); //总价input  ,更新切换
		if (current.attr('id') === 'goods-pricetype-a') {
			if (!goodcreate.cloneLeft) {
				var control_input = goodcreate.prodFormat.find('input')[0];
				control_input.click();
				control_input.click();
			}
			start_num.empty();
			saleFormatDd.empty();
			referDiv.children('dl:eq(3)').hide();

			priceFrom.html(goodcreate.clone1);
			saleNumDd.html(goodcreate.cloneLeft);
			referDiv.children('dl:eq(1)').show();
			if (goodcreate.inputsLens > 0) {
				referDiv.children('dl:eq(2)').show();
			}
			floor_price.html(floor_price_num_buffer); //底价
			tatalNum.val(goodcreate.numTotal);
		} else if (current.attr('id') === 'goods-pricetype-b') {
			if (!goodcreate.cloneRight) {
				var control_input = goodcreate.prodFormat.find('input')[0];
				control_input.click();
				control_input.click();
			}
			if (saleNumDd.children().length > 0) {
				goodcreate.clone1 = priceFrom.html(); //数量报价的价格区间
				priceFrom.empty();
			}
			referDiv.children('dl:eq(1)').hide();
			saleNumDd.empty();
			referDiv.children('dl:eq(2)').hide();
			floor_price.empty();

			saleFormatDd.html(goodcreate.cloneRight);
			referDiv.children('dl:eq(3)').show();
			start_num.html(start_num_buffer);
			tatalNum.val(goodcreate.formatTotal);
		}
	});

	/*按数量报价--价格区间*/
	goodcreate.priceFrom.live('click', function(event) {
		var len = $(this).find('tr').length;
		var current = $(event.target);
		if (current.is('a') && current.attr('class') === 'g-p-btn') {
			if (len > 4) {
				$('#goods-price-table .g-p-btn').closest('tr').remove();
				return;
			}
			goodcreate.scale++;
			var otr = $('<tr><td align="right">购买</td><td><input type="text" onkeyup="zNum(this)" onblur="zNumBig(this)" data="bn" name="pricelist_num[]"  class="{required:true,messages:{required:\'不能为空\'}}" /></td><td>件及以上：</td><td><input type="text" onblur="zPriceBig(this)" data="bp" name="pricelist_price[]" class="{required:true,messages:{required:\'不能为空\'}}" /></td><td>元/件</td><td><input type="text" onblur="wl.zWeight(this)" data="bp" name="pricelist_market_price[]" class="{required:true,messages:{required:\'不能为空\'}}" /></td><td>元/件</td><td><a  class="g-p-del">删除</a></td></tr>');
			otr.insertBefore(current.closest('tr'));
		}
		if (current.is('a') && current.attr('class') === 'g-p-del') {
			current.closest('tr').remove();
		}
	});

	/*总售过滤*/
	goodcreate.tatalNum.live('keyup', function() {
		var str = $(this).val();
		var reg = /^[1-9]\d*$/;
		if (!reg.test(str)) {
			$(this).val('');
		}
	});
	goodcreate.saleNum.find('input[data="num"]').live('keyup', function() {
		var allinputs = $('#goods-sale-num').find('input[data="num"]');
		var reg = /^\d+$/;
		var stra = $(this).val();
		var tatalNum = $('#goods-tatalNum');
		goodcreate.numTotal = 0;
		if (!reg.test(stra)) {
			$(this).val('');
		}
		allinputs.each(function(i) {
			var str = $(this).val();
			if (allinputs.eq(i).val() != '' && reg.test(str)) {
				goodcreate.numTotal += parseInt(str);
			}
		});
		tatalNum.val(goodcreate.numTotal);
	});
	goodcreate.saleFormat.find('input[data="formatnum"]').live('keyup', function() {
		var allinputs = $('#goods-sale-format').find('input[data="formatnum"]');
		var reg = /^\d+$/;
		var stra = $(this).val();
		var tatalNum = $('#goods-tatalNum');
		goodcreate.formatTotal = 0;
		if (!reg.test(stra)) {
			$(this).val('');
		}
		allinputs.each(function(i) {
			var str = $(this).val();
			if (allinputs.eq(i).val() != '' && reg.test(str)) {
				goodcreate.formatTotal += parseInt(str);
			}
		});
		tatalNum.val(goodcreate.formatTotal);
	});

	//发货方式切换
	$('#delivery_type_a').bind('click', function() {
		var shipping_id = $('#shipping_id');
		var logistics_pay = $('#logistics_pay');
		var freight_pay_wrap = $('#freight_pay_wrap');
		freight_pay_wrap.find('label.error').hide();
		logistics_pay.attr('name', '').hide();
		shipping_id.attr('name', 'freight_pay').show();
	});
	$('#delivery_type_b').bind('click', function() {
		var shipping_id = $('#shipping_id');
		var logistics_pay = $('#logistics_pay');
		var shipping_ids = $('#shipping_ids');
		var freight_pay_wrap = $('#freight_pay_wrap');
		shipping_id.children('option').removeAttr('selected');
		freight_pay_wrap.find('label.error').hide();
		shipping_id.attr('name', '').hide();
		logistics_pay.attr('name', 'freight_pay').show();
		if (shipping_ids.length > 0) {
			shipping_ids.remove();
		}
	});

	/*提交验证*/
	$('#goods_form').validate({
		errorPlacement: function(error, element) {
			element.after(error);
		},
		rules: {
			cate_id: {
				remote: {
					url: 'index.php?app=my_goods&act=check_mgcate',
					type: 'get',
					data: {
						cate_id: function() {
							return $('#cate_id').val();
						}
					}
				}
			},
			goods_name: {
				required: true
			},
			totalstock: {
				required: true,
				minlength: 1
			},
			delivery_type: {
				required: true
			},
			freight_pay: {
				required: true
			},
			delivery_time: {
				required: true
			},
			delivery_unit: {
				required: true
			},
			shipping_id: {
				required: true
			},
			start_num: {
				required: true
			}
		},
		messages: {
			cate_id: {
				remote: '请选择商品分类（必须选到最后一级）'
			},
			goods_name: {
				required: "不能为空"
			},
			totalstock: {
				required: '不能为空',
				minlength: '可售总量需要大于0'
			},
			delivery_type: {
				required: "请选择发货方式"
			},
			freight_pay: {
				required: "请选择运费"
			},
			delivery_time: {
				required: "请选择发货时间"
			},
			delivery_unit: {
				required: "请选择发货单位"
			},
			shipping_id: {
				required: '请选择模板'
			},
			start_num: {
				required: '不能为空'
			}
		}
	});
	$('#goods_form').submit(function() {
		var res = wlValidate();
		if (res[0]) {
			return true;
		} else {
			res[1].addClass('error').after('<label class="error">' + res[2] + '</label>');
			res[1].bind('keyup blur', function() {
				$(this).removeClass('error');
				$(this).next('.error').remove();
			});
			return false;
		}
	});

});

/*3.3 报价切换 html模板*/
/*销售信息-html*/
var goods_info_content = '<dl class="goods-f-c f-cb"><dt><i>*</i> <em>报价方式：</em></dt><dd><input type="radio"name="pricetype"value="1"checked="checked"id="goods-pricetype-a"/> <label for="goods-pricetype-a">按产品数量报价</label>&nbsp;&nbsp;&nbsp;&nbsp;<span style="display:none"id="pricetype_b_wrap"><input type="radio"name="pricetype"value="2"id="goods-pricetype-b"/><label for="goods-pricetype-b">按产品规格报价</label></span></dd></dl><dl class="goods-f-tab f-cb"><dt><i>*</i> <em>价格区间：</em></dt><dd><table id="goods-price-table"><tr><th width="40" align="right">&nbsp;</th><th width="80">购买数量</th><th width="60">&nbsp;</th><th width="80">商品零售价</th><th width="60">&nbsp;</th><th width="80">商品市场价</th><th>&nbsp;</th><th width="40">&nbsp;</th></tr><tr><td align="right">起订量</td><td><input type="text" name="pricelist_num[]"  id="orderNum" class="{required:true,messages:{required:\'不能为空\'}}" onkeyup="zNum(this)" /></td><td>件及以上：</td><td><input type="text" name="pricelist_price[]" id="orderPrice" class="{required:true,messages:{required:\'不能为空\'}}" onblur="creat_market_price(this)" /></td><td>元/件</td><td><input type="text" name="pricelist_market_price[]" class="{required:true,messages:{required:\'不能为空\'}}" onblur="wl.zWeight(this)" /></td><td> 元/件</td><td>&nbsp;</td></tr><tr><td colspan="8"><a class="g-p-btn">增加价格区间</a></td></tr></table></dd></dl><dl class="goods-f-tab f-cb"id="goods-sale-num"><dt><i>*</i> <em>可售数量：</em></dt><dd></dd></dl><dl class="goods-f-tab f-cb"id="goods-sale-format"><dt><i>*</i> <em>可售数量：</em></dt><dd></dd></dl><dl class="goods-f-c f-cb"id="goods_code"><dt><i>&nbsp;</i> <em>商品货号：</em></dt><dd><input type="text"name="goods_code"class="width_normal"/></dd></dl><dl class="goods-f-c f-cb f-dn"id="floor_price"><dt><i>*</i> <em>商品底价：</em></dt><dd><input type="text"name="floor_price_num"class="width_normal"onblur="wl.zWeight(this)"/></dd></dl><dl class="goods-f-c f-cb"id="start_num"></dl><dl class="goods-f-c f-cb"><dt><i id="zl">*</i> <em>可售总量：</em></dt><dd><input type="text"name="totalstock"id="goods-tatalNum"style="background:#fff;border:1px solid #999;text-align: center;"/> 件&nbsp;&nbsp;&nbsp;&nbsp;<span style="display:none;">可售总量是各规格可售数量的总和</span></dd></dl>';
/*价格区间*/
var priceFrom_content = '<tr><th width="40" align="right">&nbsp;</th><th width="80">购买数量</th><th width="60">&nbsp;</th><th width="80">商品零售价</th><th width="60">&nbsp;</th><th width="80">商品市场价</th><th>&nbsp;</th><th width="40">&nbsp;</th></tr><tr><td align="right">起订量</td><td><input type="text" name="pricelist_num[]"  id="orderNum" class="{required:true,messages:{required:\'不能为空\'}}" onkeyup="zNum(this)" /></td><td>件及以上：</td><td><input type="text" name="pricelist_price[]" id="orderPrice" class="{required:true,messages:{required:\'不能为空\'}}" onblur="creat_market_price(this)" /></td><td>元/件</td><td><input type="text" name="pricelist_market_price[]" class="{required:true,messages:{required:\'不能为空\'}}" onblur="wl.zWeight(this)" /></td><td> 元/件</td><td>&nbsp;</td></tr><tr><td colspan="8"><a class="g-p-btn">增加价格区间</a></td></tr>';
/*商品货号*/
var goods_code_content = '<dt><i>&nbsp;</i><em>商品货号：</em></dt><dd><input type="text" name="goods_code" class="width_normal"></dd>';
/*商品底价-数量*/
var floor_price_content = '<dt><i>*</i> <em>商品底价：</em></dt><dd><input type="text" name="floor_price_num" class="width_normal" onblur="wl.zWeight(this)" /></dd>';
/*起订量-规格*/
var start_num_content = '<dt><i>*</i> <em>起订量：</em></dt><dd><input type="text" name="start_num" onkeyup="zNum(this)" /></dd>';

/* 商品分类选择函数-begin */
function gcategoryInit(divId) {
	$("#" + divId + " > select").get(0).onchange = gcategoryChange; // select的onchange事件
	//屏蔽jquery报错
	window.onerror = function() {
		return true;
	};
	// $("#" + divId + " .edit_gcategory").click(gcategoryEdit); // 编辑按钮的onclick事件
}

function gcategoryChange() {
	// 删除后面的select
	$(this).nextAll("select").remove();

	// 计算当前选中到id和拼起来的name
	var selects = $(this).siblings("select").andSelf();
	var id = 0;
	var names = new Array();
	var i;
	var goods_attr_list = $("#goods-attr-list"); //属性容器dl
	var goods_property = $('#goods-property'); //属性填充dd
	var goods_spec_list = $('#goods-spec-list'); //规格容器tr
	var goods_format_prod = $("#goods-format-prod"); //规格填充td
	var goods_info_wrap = $('#goods_info_wrap'); //销售信息容器div
	for (i = 0; i < selects.length; i++) {
		sel = selects[i];
		if (sel.value > 0) {
			id = sel.value; //option的value
			name = sel.options[sel.selectedIndex].text; //option的文字
			names.push(name);
		}
	}
	$(".mls_id").val(id);
	// $(".mls_name").val(name);   //不存在此对象
	$(".mls_names").val(names.join("\t"));

	// ajax请求下级分类
	if (this.value > 0) {
		var _self = this;
		var url = REAL_SITE_URL + '/index.php?app=my_goods&act=AjaxSpecAttr';
		$.getJSON(url, {
			'cate_id': this.value
		}, function(data) {
			if (data.done) {
				if (data.retval.cate.length > 0) {
					$("<select><option>" + lang.select_pls + "</option></select>").change(gcategoryChange).insertAfter(_self);
					var cate = data.retval.cate;
					for (i = 0; i < cate.length; i++) {
						$(_self).next("select").append("<option value='" + cate[i].cate_id + "'>" + cate[i].cate_name + "</option>");
					}
				}
				//属性值个数大于0  显示属性区域
				if (data.retval.attribute.length > 0) {
					goods_property.empty().html(data.retval.attribute); //属性区域dl的dd加入后台str
					goods_attr_list.show(); //属性区域dl
				} else {
					goods_attr_list.hide();
				}
				//规格值个数大于0  显示规格区域
				if (data.retval.spec.length > 0) {
					goods_format_prod.empty().html(data.retval.spec); //规格区域td 加入后台str | 事件绑定
					goods_spec_list.show(); //规格区域tr
				} else {
					goods_spec_list.hide();
				}
			} else {
				alert(data.msg);
			}
		});
	} else {
		goods_attr_list.hide();
		goods_spec_list.hide();
	}
	goods_info_wrap.empty().html(goods_info_content);
}
/*end*/

function wlValidate() {
	var next = true;
	var error = [true, null, ''],
		i, len;
	var o4 = $('#wt');
	var o5 = $('input[name="gweight"]');
	var o8 = $('select[name="sgcate_id[]"]');
	var freg = /^[1-9]+\d*$|^[1-9]+\d*\.\d{1,3}$|^\d\.\d{1,3}$/;
	if (o8.val() == "0" && next) {
		o8.get(0).focus();
		error = [false, o8, "请选择店铺分类"];
		next = false;
	}
	if (o4.css('display') == 'inline' && next && !freg.test(o5.val())) {
		error = [false, o5, "不能为空"];
		o5.get(0).focus();
		next = false;
	}
	return error;
}

function zNum(o) {
	var v = $(o).val();
	var freg = /^[1-9]+\d*$|^[1-9]+\d*\.\d{1,2}$|^\d\.\d{1,2}$/;
	if (!freg.test(v)) {
		$(o).val('');
	}
}

/*价格区间过滤*/
function creat_market_price(o) {
	var v = $(o).val();
	var freg = /^[1-9]+\d*$|^[1-9]+\d*\.\d{1,2}$|^\d\.\d{1,2}$/;
	if (!freg.test(v)) {
		$(o).val('');
	} else {
		$(o).parent('td').nextAll('td').each(function() {
			var input_name = $(this).children('input').attr('name')!=undefined?$(this).children('input').attr('name'):'';
			if (input_name.search(/market_price/) != -1) {   
				var cur_val = parseFloat(v);
				if (v.search(/\./) != -1) {
					$(this).children('input').val(wl.number_format(cur_val * 1.2, 2));
				} else {
					$(this).children('input').val(wl.number_format(cur_val * 1.2, 0));
				}
			}
		});
	}
}

function zNumBig(o) {
	var v = $(o).val();
	var refer = $.trim($('#orderNum').val());
	var freg = /^[1-9]+\d*$|^[1-9]+\d*\.\d{1,2}$|^\d\.\d{1,2}$/;
	if (!refer) {
		$('#orderNum').get(0).focus();
		$(o).val('');
		window.alert('起订量不能为空');
	} else {
		if (freg.test(v)) {
			var pinput = $(o).closest('tr').prev('tr').find('input[data="bn"]').val();
			if (parseInt(refer) >= parseInt(v)) {
				$(o).val('');
				window.alert('输入值需大于起订量');
				o.focus();
			} else if (pinput && parseInt(pinput) >= parseInt(v)) {
				$(o).val('');
				window.alert('输入值需大于上一个购买量');
				o.focus();
			}
		} else {
			$(o).val('');
		}

	}
}

function zPriceBig(o) {
	var v = $(o).val();
	var refer = $.trim($('#orderPrice').val());
	var freg = /^[1-9]+\d*$|^[1-9]+\d*\.\d{1,2}$|^\d\.\d{1,2}$/;
	if (!refer) {
		$('#orderPrice').get(0).focus();
		$(o).val('');
		window.alert('起订量价格不能为空');
	} else {
		if (freg.test(v)) {
			var pinput = $(o).closest('tr').prev('tr').find('input[data="bp"]').val();
			if (parseInt(v) >= parseInt(refer)) {
				$(o).val('');
				window.alert('输入值需小于起订量价格');
				o.focus();
			} else if (pinput && parseInt(v) >= parseInt(pinput)) {
				$(o).val('');
				window.alert('输入值需小于上一个价格');
				o.focus();
			} else {
				creat_market_price(o);
			}
		} else {
			$(o).val('');
		}
	}
}